﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLSanBay : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdSanBay.DataSource = SanBayDA.Inst.GetAll(true);
            grdSanBay.DataBind();
            if (grdSanBay.PageCount <= 1)
            {
                grdSanBay.PagerStyle.Visible = false;
            }
        }
        private void Load_ListTP()
        {
            List<ThanhPho> list = new List<ThanhPho>();
            list = ThanhPhoDA.Inst.GetAll(true);
            DropTP.DataSource = list;
            DropTP.DataTextField = "TenSanBay";
            DropTP.DataValueField = "MaSanBay";
            DropTP.DataBind();
            DropTP.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        private void ResetControlValues()
        {
            txtTenSB.Text = "";
            txtMaSB.Text = "";
            DropTP.SelectedIndex = -1;
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            ResetControlValues();
            Insert = true;
            Load_ListTP();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SanBay SB = new SanBay ();
                SB.MaSanBay = txtMaSB.Text;
                SB.TenSanBay = txtTenSB.Text;
                SB.ItemThanhPho = new ThanhPho () { MaThanhPho  = DropTP.SelectedValue };
                
                if (Insert == true)
                {
                    SanBayDA.Inst.Insert(SB);
                }
                else
                {
                    SanBayDA.Inst.Update(SB);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdSanBay_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemSB = SanBayDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaSB.Text = itemSB.MaSanBay.ToString();
                    txtMaSB.ReadOnly = true;
                    txtTenSB.Text = itemSB.TenSanBay.ToString();
                    Load_ListTP();
                    DropTP.SelectedValue = itemSB.ItemThanhPho.MaThanhPho;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    SanBayDA.Inst.Delete(itemSB);
                    LoadGrid();
                    break;
            }
        }

        protected void grdSanBay_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var TP = (SanBay)e.Item.DataItem;
                var lbl = e.Item.FindControl("lblTenTP") as Label;
                lbl.Text = TP.ItemThanhPho.TenThanhPho;                    
            }
        }

        protected void grdSanBay_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdSanBay.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}